#
#	genx_index.py
#
#	David Janes
#	2008.08.16
#
#	GenX - search, create and insert structured data
#

import sys
import types
import re
import pprint
import cgi
import md5

import wsgiref.handlers
import wsgiref.headers

import bm_text

class IndexMixin:
	def __init__(self):
		pass

	def ActionIndex(self):
		self.headers.add_header('Content-type','text/html', charset = 'utf-8')
		self.start('200 OK', self.headers.items())

		for part in self._ActionIndex():
			if type(part) == types.UnicodeType:
				part = part.encode('utf-8')

			yield part

	def _ActionIndex(self):
		yield self.StandardHeader(title = "Choose Wisely")
		yield """
<h1>GenX</h1>
<div id="id_form">
<div id="id_options">
<form action="?" id="id_form_itself">
<input type="hidden" name="action" value="search" />
<input type="hidden" name="original" value="%(li_original)s" />
<input type="hidden" name="mid" id="id_mid" value="" />
<input type="hidden" name="callbacks" id="id_callbacks" value="%(callbacks)s" />
<h2>
Look for &hellip;
</h2>
<p>
<input type="text" name="search" value="%(li_search)s" class="search"/>
</p>

<h2>
In &hellip;
</h2>
<ul>
""" % {
	"li_original" : bm_text.unicode2html(self.p_original),
	"li_search" : bm_text.unicode2html(self.p_search),
	"callbacks" : self.p_callbacks,
}

		for searchd in self.searchds:
			searchd["$search"] = self.p_search
			searchd["$in"] = searchd.get('source') and " (%s)" % searchd.get('source') or ""
			yield """
<li>
	<a href="javascript:index_js.submit('%(mid)s')">%(title)s</a>%($in)s
</li>""" % searchd

		"""
	<div class="option" id="id_option_%(mid)s">
			<input type="hidden" name="mid" value="%(mid)s" />
			<input type="hidden" name="action" value="search" />
			<label for="id_%(mid)s">
				Find %(title)s:
			</label>
			<div class="option">
				<input type="text" name="search" id="id_%(mid)s" value="%($search)s" />
				<button type="submit">%(title)s</button>
			</div>
		</form>
	</div>""" % searchd

		yield """
</ul></form></div></div>
<script type="text/javascript">
index_js = {
	submit : function(mid) {
		document.getElementById('id_mid').value = mid;
		document.getElementById('id_form_itself').submit();
	},

	end : 0
};
</script>
"""
		yield self.StandardFooter()

